Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update precision in the ONNX strided_slice, update precision of ToScalar #6272

Merged
merged 2 commits into from
Aug 14, 2020

Conversation

mbrookhart
Copy link
Contributor

Fixes #6263

ToScalar was casting a 64 bit Int to a 64 bit float, which reduced precision too much. I switched things to use int64_t/long double where needed to keep precision.

Thanks!

cc: @kevinthesun @zhiics @yongwww @lixiaoquan

@@ -374,7 +374,7 @@ inline bool IsEqualScalar(const Expr& a, const Expr& b) {
* \param i element index
* \return Converted scalar value.
*/
static inline double ToScalar(const runtime::NDArray& array, size_t i = 0) {
static inline long double ToScalar(const runtime::NDArray& array, size_t i = 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why double is not sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a double only has 52 bits of mantissa, it can't store the full precision of an int64_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we get rounding errors if we pass in large int64_t values

Copy link
Contributor Author

@mbrookhart mbrookhart Aug 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On x86, long double has 63 bits of mantissa and 1 bit of sign, just like int64. On PowerPC and ARM, it's a 128bit floating point with 106 bits of mantissa.

@zhiics zhiics merged commit 8d91058 into apache:master Aug 14, 2020
@zhiics
Copy link
Member

zhiics commented Aug 14, 2020

Thanks @mbrookhart

@mbrookhart mbrookhart deleted the mbrookhart/precise_to_scalar branch August 14, 2020 17:29
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…lar (apache#6272)

* Update precision in the ONNX strided_slice, update precision of ToScalar

* fix tests
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…lar (apache#6272)

* Update precision in the ONNX strided_slice, update precision of ToScalar

* fix tests
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…lar (apache#6272)

* Update precision in the ONNX strided_slice, update precision of ToScalar

* fix tests
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Sep 2, 2020
…lar (apache#6272)

* Update precision in the ONNX strided_slice, update precision of ToScalar

* fix tests
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Sep 3, 2020
…lar (apache#6272)

* Update precision in the ONNX strided_slice, update precision of ToScalar

* fix tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#4312 broke Huggingface BERT ONNX import
2 participants